<?php

$DB = Antz::registry('db');
global $gCms;

error_reporting(E_ALL);

$this->ThrowConfigExceptions = false;

$dirname = dirname(__FILE__);
$dirname = str_replace(DIRECTORY_SEPARATOR, '/', $dirname);

// remove our pages
$aliases = array(
    $this->GetConfig('login_id'),
    $this->GetConfig('logout_id'),
    $this->GetConfig('profile_id'),
    $this->GetConfig('register_id'),
    $this->GetConfig('email_verification_id'),
    $this->GetConfig('tnc_id'),
    $this->GetConfig('lost_password_id')
);

$contentops = $gCms->GetContentOperations();

// delete the pages that we created with install
foreach($aliases as $k=>$v){
    if($contentobj = $contentops->LoadContentFromId($v, false)){
        $contentobj->Delete();
    };
    $DB->delete(cms_db_prefix().'iusers_pages_to_groups', "page_id = '{$v}'");
};

//$DB->debug(true);
//$DB->showErrors(true);
//$haltInstall = true;


$this->removeHooksFromCore();

// backup our existing data

if($gCms->config['dbms']=='mysql'){
    // mysql commands
    if($DB->countRows(cms_db_prefix().'iusers', '1=1')>0){
        if(file_exists($dirname.'/bup_users.sql')) unlink($dirname.'/bup_users.sql');
        $DB->query("SELECT * INTO OUTFILE '{$dirname}/bup_users.txt' FROM {$this->config['db_prefix']}iusers");
    };
    if($DB->countRows(cms_db_prefix().'iusers_groups', '1=1')>0){
        if(file_exists($dirname.'/bup_groups.sql')) unlink($dirname.'/bup_groups.sql');
        $DB->query("SELECT * INTO OUTFILE '{$dirname}/bup_groups.txt' FROM {$this->config['db_prefix']}iusers_groups");
    };
    if($DB->countRows(cms_db_prefix().'iusers_to_groups', '1=1')>0){
        if(file_exists($dirname.'/bup_users_to_groups.sql')) unlink($dirname.'/bup_users_to_groups.sql');
        $DB->query("SELECT * INTO OUTFILE '{$dirname}/bup_users_to_groups.txt' FROM {$this->config['db_prefix']}iusers_to_groups");
    };
    if($DB->countRows(cms_db_prefix().'iusers_pages_to_groups', '1=1')>0){
        if(file_exists($dirname.'/bup_users_to_groups.sql')) unlink($dirname.'/bup_users_to_groups.sql');
        $DB->query("SELECT * INTO OUTFILE '{$dirname}/bup_pages_to_groups.txt' FROM {$this->config['db_prefix']}iusers_pages_to_groups");
    };
    if($DB->countRows(cms_db_prefix().'iusers_history', '1=1')>0){
        if(file_exists($dirname.'/bup_history.sql')) unlink($dirname.'/bup_history.sql');
        $DB->query("SELECT * INTO OUTFILE '{$dirname}/bup_history.txt' FROM {$this->config['db_prefix']}iusers_history");
    };
    if($DB->countRows(cms_db_prefix().'iusers_config', '1=1')>0){
        if(file_exists($dirname.'/bup_config.sql')) unlink($dirname.'/bup_config.sql');
        $DB->query("SELECT * INTO OUTFILE '{$dirname}/bup_config.txt' FROM {$this->config['db_prefix']}iusers_config");
    };
    if($DB->countRows(cms_db_prefix().'iusers_countries', '1=1')>0){
        if(file_exists($dirname.'/bup_countries.sql')) unlink($dirname.'/bup_countries.sql');
        $DB->query("SELECT * INTO OUTFILE '{$dirname}/bup_countries.txt' FROM {$this->config['db_prefix']}iusers_countries");
    };
    if($DB->countRows(cms_db_prefix().'iusers_cities', '1=1')>0){
        if(file_exists($dirname.'/bup_cities.sql')) unlink($dirname.'/bup_cities.sql');
        $DB->query("SELECT * INTO OUTFILE '{$dirname}/bup_cities.txt' FROM {$this->config['db_prefix']}iusers_cities");
    };
    // end of mysql statements
}else if($gCms->config['dbms']=='postgres7'){
    // postgres commands
    if($DB->countRows(cms_db_prefix().'iusers', '1=1')>0){
        if(file_exists($dirname.'/bup_users.sql')) unlink($dirname.'/bup_users.sql');
        $DB->query("COPY {$this->config['db_prefix']}iusers TO '{$dirname}/bup_users.txt'");
    };
    if($DB->countRows(cms_db_prefix().'iusers_groups', '1=1')>0){
        if(file_exists($dirname.'/bup_groups.sql')) unlink($dirname.'/bup_groups.sql');
        $DB->query("COPY {$this->config['db_prefix']}iusers_groups TO '{$dirname}/bup_groups.txt'");
    };
    if($DB->countRows(cms_db_prefix().'iusers_to_groups', '1=1')>0){
        if(file_exists($dirname.'/bup_users_to_groups.sql')) unlink($dirname.'/bup_users_to_groups.sql');
        $DB->query("COPY {$this->config['db_prefix']}iusers_to_groups TO '{$dirname}/bup_users_to_groups.txt'");
    };
    if($DB->countRows(cms_db_prefix().'iusers_pages_to_groups', '1=1')>0){
        if(file_exists($dirname.'/bup_users_to_groups.sql')) unlink($dirname.'/bup_users_to_groups.sql');
        $DB->query("COPY {$this->config['db_prefix']}iusers_pages_to_groups TO '{$dirname}/bup_pages_to_groups.txt'");
    };
    if($DB->countRows(cms_db_prefix().'iusers_history', '1=1')>0){
        if(file_exists($dirname.'/bup_history.sql')) unlink($dirname.'/bup_history.sql');
        $DB->query("COPY {$this->config['db_prefix']}iusers_history TO '{$dirname}/bup_history.txt'");
    };
    if($DB->countRows(cms_db_prefix().'iusers_config', '1=1')>0){
        if(file_exists($dirname.'/bup_config.sql')) unlink($dirname.'/bup_config.sql');
        $DB->query("COPY {$this->config['db_prefix']}iusers_config TO '{$dirname}/bup_config.txt'");
    };
    if($DB->countRows(cms_db_prefix().'iusers_countries', '1=1')>0){
        if(file_exists($dirname.'/bup_countries.sql')) unlink($dirname.'/bup_countries.sql');
        $DB->query("COPY {$this->config['db_prefix']}iusers_countries TO '{$dirname}/bup_countries.txt'");
    };
    if($DB->countRows(cms_db_prefix().'iusers_cities', '1=1')>0){
        if(file_exists($dirname.'/bup_cities.sql')) unlink($dirname.'/bup_cities.sql');
        $DB->query("COPY {$this->config['db_prefix']}iusers_cities TO '{$dirname}/bup_cities.txt'");
    };
    // end of mysql postgres statements
};

$DB->query('DROP TABLE IF EXISTS '.cms_db_prefix().'iusers');
$DB->query('DROP TABLE IF EXISTS '.cms_db_prefix().'iusers_groups');
$DB->query('DROP TABLE IF EXISTS '.cms_db_prefix().'iusers_pages_to_groups');
$DB->query('DROP TABLE IF EXISTS '.cms_db_prefix().'iusers_to_groups');
$DB->query('DROP TABLE IF EXISTS '.cms_db_prefix().'iusers_history');
$DB->query('DROP TABLE IF EXISTS '.cms_db_prefix().'iusers_config');
$DB->query('DROP TABLE IF EXISTS '.cms_db_prefix().'iusers_countries');
$DB->query('DROP TABLE IF EXISTS '.cms_db_prefix().'iusers_cities');

if($gCms->config['dbms']=='postgres7'){
    $DB->query('DROP SEQUENCE '.cms_db_prefix().'iusers_id_seq');
    $DB->query('DROP SEQUENCE '.cms_db_prefix().'iusers_groups_id_seq');
    $DB->query('DROP SEQUENCE '.cms_db_prefix().'iusers_history_id_seq');
    $DB->query('DROP SEQUENCE '.cms_db_prefix().'iusers_cities_id_seq');
}


$this->RemovePermission($this->GetName().' Config');
$this->RemovePermission($this->GetName().' Users');
$this->RemovePermission($this->GetName().' Groups');
$this->RemovePermission($this->GetName().' Places');

if($haltInstall) exit;
